package com.yourenbang.lingyun.transaction.web.controller.pay;

import com.alibaba.fastjson.JSONObject;
import com.yourenbang.lingyun.constants.AppId;
import com.yourenbang.lingyun.constants.PubParamCode;
import com.yourenbang.lingyun.constants.enums.ChannelIdEnums;
import com.yourenbang.lingyun.core.base.controller.BaseController;
import com.yourenbang.lingyun.core.base.interceptor.ExcludeInterceptor;
import com.yourenbang.lingyun.core.message.JLResult;
import com.yourenbang.lingyun.core.message.Request;
import com.yourenbang.lingyun.core.message.Result;
import com.yourenbang.lingyun.core.model.vo.PageVO;
import com.yourenbang.lingyun.core.util.BizAssertUtils;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.domain.PubParam;
import com.yourenbang.lingyun.mapper.PubParamMapper;
import com.yourenbang.lingyun.transaction.web.domain.dto.BalanceListDTO;
import com.yourenbang.lingyun.transaction.web.domain.dto.KlksBalanceListDTO;
import com.yourenbang.lingyun.transaction.web.domain.dto.KlksBalanceListDetailDTO;
import com.yourenbang.lingyun.transaction.web.domain.dto.TradeListDTO;
import com.yourenbang.lingyun.transaction.web.domain.dto.pay.jlpay.TradePushDTO;
import com.yourenbang.lingyun.transaction.web.domain.vo.BalanceListVO;
import com.yourenbang.lingyun.transaction.web.service.manage.ManageTradeService;
import com.yourenbang.lingyun.transaction.web.service.pay.TradeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <h3>service-transaction</h3>
 * <p>明细查询</p>
 *
 * @author : jason
 * @date : 2020-08-26 14:33
 **/
@Slf4j
@RestController
@RequestMapping("/transaction")
public class TradeController extends BaseController {

    @Autowired
    private TradeService tradeService;
    @Autowired
    private PubParamMapper pubParamMapper;
    @Autowired
    ManageTradeService manageTradeService;
    /**
     * 查询余额明细
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/5022">火眼接口连接</a>
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/3014">火眼接口连接</a>
     */
    @PostMapping(value = "/balance/list")
    public Result<PageVO> balanceList(@Validated @RequestBody Request<BalanceListDTO> dtoRequest, BindingResult br) {
        KLog.info("[查询余额明细]入参:{}", dtoRequest);
        Result result = verifyParam(br);
        if (!result.isSuccess()) {
            return result;
        }
        BalanceListDTO listDTO = dtoRequest.getData();
        listDTO.setAppId(dtoRequest.getAppId());
        return tradeService.queryBalanceList(listDTO);
    }

    /**
     * 查询余额明细V2新版本
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/14624">火眼接口连接</a>
     */
    @PostMapping(value = "/balance/list/v2")
    public Result<PageVO> balanceListV2(@Validated @RequestBody Request<BalanceListDTO> dtoRequest, BindingResult br) {
        KLog.info("[查询余额明细V2]入参:{}", dtoRequest);
        Result result = verifyParam(br);
        if (!result.isSuccess()) {
            return result;
        }
        BalanceListDTO listDTO = dtoRequest.getData();
        listDTO.setAppId(dtoRequest.getAppId());
        return tradeService.queryBalanceListV2(listDTO);
    }

    /**
     * 列出用户的账单类型
     * @param dtoRequest 请求参数
     * @return 账单类型列表
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/14625">火眼地址</a>
     */
    @PostMapping(value = "/ally/fund/bill/type")
    public Result<List<JSONObject>> allyFundBillType(@RequestBody Request<BalanceListDTO> dtoRequest) {
        return Result.builder().success(tradeService.allyFundBillType());
    }

    /**
     * 快收查询余额明细
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/5030">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @PostMapping(value = "/balance/list/klks")
    public Result<PageVO> balanceListKlks(@Validated @RequestBody Request<KlksBalanceListDTO> dtoRequest, BindingResult br) {
        KLog.info("[快收查询余额明细]入参:{}", dtoRequest);
        Result result = verifyParam(br);
        if (!result.isSuccess()) {
            return result;
        }
        KlksBalanceListDTO listDTO = dtoRequest.getData();
        listDTO.setAppId(StringUtils.isBlank(listDTO.getAppId())?dtoRequest.getAppId():listDTO.getAppId());
        Result<PageVO> balanceListResult = tradeService.queryBalanceListKlks(listDTO);
        return balanceListResult;
    }

    /**
     * 快收查询余额明细详情
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/5414">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @PostMapping(value = "/balance/list/detail/klks")
    public Result<BalanceListVO> balanceListDetailKlks(@Validated @RequestBody Request<KlksBalanceListDetailDTO> dtoRequest, BindingResult br) {
        KLog.info("[快收查询余额明细详情]入参:{}", dtoRequest);
        Result result = verifyParam(br);
        if (!result.isSuccess()) {
            return result;
        }
        BalanceListVO balanceListDetail = tradeService.queryBalanceListDetailKlks(dtoRequest.getData());
        return Result.builder().success(balanceListDetail);
    }


    /**
     * 查询交易明细详情
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/3126">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @PostMapping(value = "/trade/list")
    public Result tradeList(@Validated @RequestBody Request<TradeListDTO> dtoRequest, BindingResult br) {
        KLog.info("[交易明细]入参:{}", dtoRequest);
        Result result = verifyParam(br);
        if (!result.isSuccess()) {
            return result;
        }
        //兼容老版本不传channelid
        if (StringUtils.equals(dtoRequest.getAppId(), AppId.KLKS.getAppId())) {
            dtoRequest.getData().setChannelId(ChannelIdEnums.mpos.getCode());
        }else if (StringUtils.equals(dtoRequest.getAppId(), AppId.KLKS_PRO.getAppId())){
            //pro查所有电签交易
            dtoRequest.getData().setChannelId(ChannelIdEnums.epos.getCode());
        }
            Result<PageVO> pageVOResult = tradeService.queryTradeDetail(dtoRequest.getData());
            KLog.info("[交易明细]入参:{},结果:{}", dtoRequest,pageVOResult);
            return pageVOResult;
    }

    /**
     * 模拟回调jl
     * <p>https://huoyan.kaolakuaishou.cn/project/97/interface/api/2950</p>
     * @param dto
     * @return
     */
    @ExcludeInterceptor
    @PostMapping(value = "/jlpay/callback/transaction")
    public JLResult jlpayTradePush(@RequestBody TradePushDTO dto) {
        KLog.info("[jlpay交易流水]入参:{}", dto.toJSON());
        return JLResult.success();
    }

    @ExcludeInterceptor
    @PostMapping(value = "/m40/token/update")
    public Result updateM40Token(@RequestParam String token) {
        KLog.info("[M40交易拉取Token更新]入参:{}", token);
        PubParam pubParam = new PubParam();
        pubParam.setCode(PubParamCode.M40_TRADE_TOKEN);
        pubParam.setStrVal(token);
        int updateTokenResult = pubParamMapper.updateByPrimaryKeySelective(pubParam);
        BizAssertUtils.assertOne(updateTokenResult, "更新token失败");
        return Result.builder().success();
    }

    @ExcludeInterceptor
    @GetMapping(value = "/m40/file/push")
    public Result tradeFileNamePush(@RequestParam String fileName) {
        KLog.info("[M40交易文件名推送]入参:{}", fileName);
        //http://60.205.158.73/20220312111657907_20220312-153116.zip
        if (manageTradeService.addM40TradeFileToRedis("http://60.205.158.73/" + fileName)) {
            return Result.builder().success();
        } else {
            KLog.error("M40交易文件名推送,添加redis失败,{}", fileName);
            return Result.builder().fail();
        }
    }
}
